<?php 
require_once( dirname(__FILE__) . '/../../inc/Database.inc');
require_once( dirname(__FILE__) . '/../../inc/layout.inc');
require_once( dirname(__FILE__) . '/../adminLayout.inc');
require_once( dirname(__FILE__) . '/../../fckeditor/fckeditor.php');
require_once( dirname(__FILE__) . '/../class.AdminQueries.php');
require_once( dirname(__FILE__) . '/../../inc/classes/class.HomePage.php');

HTMLHeader(true);
$conn = ConnectToDatabase2();

?>
		<div id="mainWrapper">
			<div id="main">
				<?php 
					$page = $_POST['admin'];
					printHeader(); 
					printAdminNavigation($page);
				?>
				<div id="mainContent">
					<?php 
					//echo $_SERVER['REQUEST_METHOD'];
					if($_SERVER['REQUEST_METHOD'] == 'POST' && 
					   ((isset($_POST['hChoice']) && count($_POST['hChoice']) <= 10) ||
					   (isset($_POST['action']) && count($_POST['action']) <= 10))
					  ){
						
						if(isset($_POST['hChoice'])) {
							
							switch($_POST['hChoice']) {
								case 'new':
								?>
								<script type="text/javascript" src="/cureadmin/home/home.js"></script>
								<form method="post">
								<?php
									$oFCKeditor = new FCKeditor('Home') ;
									$oFCKeditor->BasePath = '../../fckeditor/' ;
									$oFCKeditor->ToolbarSet = 'Default'; //MainHeader
									$oFCKeditor->Height = '300';
									$oFCKeditor->Width = '780';
									$oFCKeditor->Value = '';
									$oFCKeditor->Create();
								?>
									<div style="text-align: center;">
										<input type="submit" value="Save">
									</div>
									<input type="hidden" name="action" value="editC">
								</form>
								<?php 
									break;
								case 'edit':
									$homePageEntries = AdminQueries::getAllHomePageEntries();
										?>
										<form method="post">
											<select name="hEntry" size="10">
												<?php 
													foreach ($homePageEntries as $entry) {
														$optionText = strip_tags($entry->get_homePageHTML());
														$day = substr($entry->get_timestamp(), 8, 2);
														$month = substr($entry->get_timestamp(), 5, 2);
														$year = substr($entry->get_timestamp(), 0, 4);
														$hour = substr($entry->get_timestamp(), 11, 2);
														$minute = substr($entry->get_timestamp(), 14, 2);
														
														$date = date("F j, Y, g:i a", mktime($hour, $minute, 0, $month, $day, $year));
														
														if(strlen($optionText) > 60) {
															$optionText = substr($optionText, 0, 60);
															$optionText .= "...";
														}
												?>
													<option value="<?php echo $entry->get_homePageID();?>"><?php echo $date." - $optionText";?></option>
												<?php }?>
											</select>
											<input type="hidden" name="action" value="editE">
											<input type="hidden" name="admin" value="mHome">
											<div>
												<input type="submit" value="OK">
											</div>
										</form>
										<?php
									break;	
							}
						}
						elseif(isset($_POST['action'])){
							
								switch($_POST['action']){
									case 'editC':
										if(AdminQueries::insertHomePage($_POST['Home'])) {
											echo "Home page saved!";
										}
										else {
											echo "Home page was not saved!";
										}
										break;
									case 'editE':
										$homePageID = $_POST['hEntry'];
										$entry = AdminQueries::getHomePageEntry($homePageID);
										?>
										<script type="text/javascript" src="/cureadmin/home/home.js"></script>
										<form method="post" id="goBackForm">
											<span id="goBack" style="text-decoration: underline; color: blue; cursor: pointer">&lt;&lt; Back to list</span>
											<input type="hidden" name="admin" value="mHome">
											<input type="hidden" name="hChoice" value="edit">
										</form>
										<form method="post">
										<?php 
										$oFCKeditor = new FCKeditor('Home') ;
										$oFCKeditor->BasePath = '../../fckeditor/' ;
										$oFCKeditor->ToolbarSet = 'Default';//'MainHeader';
										$oFCKeditor->Height = '300';
										$oFCKeditor->Width = '780';
										$oFCKeditor->Value = $entry->get_homePageHTML();
										$oFCKeditor->Create();
										?>
											<input type="hidden" name="action" value="editC">
											<input type="hidden" name="admin" value="mHome">
											<input type="hidden" name="hid" value="<?php echo $homePageID;?>">
											<div style="text-align: center;">
												<input type="submit" value="Save">
											</div>
										</form>
										<?php
										break;
									case 'editU':
										$homePageID = $_POST['hid'];
										$homePageEntry = $_POST['Home'];
										if(AdminQueries::updateHomePage($homePageID, $homePageEntry)) {
											echo "Home page saved!";
										}
										else {
											echo "Home page was not saved!";
										}
										break;
								}
						}
					  }
					else {
						printErrorPage();
					}
					?>
				</div>
				<?php 
				printFooter();
				?>
			</div> <!-- main -->
		</div> <!-- mainWrapper -->
<?php 
HTMLFooter();
?>